package com.sunday.volunteer.ui.user;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sunday.common.event.EventBus;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;
import com.sunday.member.common.MemberConst;
import com.sunday.member.event.ExitApp;
import com.sunday.member.utils.SharePerferenceUtils;
import com.sunday.volunteer.BaseApp;
import com.sunday.volunteer.R;
import com.sunday.volunteer.http.HttpClient;
import com.sunday.volunteer.utils.BaseUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Pual's PC on 2016/10/14.
 */
public class UpdatePasswordActivity extends BaseActivity {

    @Bind(R.id.password)
    EditText password;
    @Bind(R.id.oldpassword)
    EditText oldpassword;
    @Bind(R.id.title_text)
    TextView title_text;
    @Bind(R.id.btn)
    Button btn;

    private String mobile;
    private String validateCode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_password);
        ButterKnife.bind(this);
        title_text.setText("修改密码");

        if(getIntent().getIntExtra(BaseUtil.TYPE , -1)  != 1){
            oldpassword.setVisibility(View.GONE);
        }
        mobile = getIntent().getStringExtra(BaseUtil.MOBILE);
        validateCode = getIntent().getStringExtra(BaseUtil.CODE);
        //password.addTextChangedListener(userWatcher);
    }

        @OnClick(R.id.btn)
        void Btn(){
            if(getIntent().getIntExtra(BaseUtil.TYPE , -1)  == 1){
                if(TextUtils.isEmpty(oldpassword.getText().toString())){
                    ToastUtils.showToast(mContext , "请输入旧密码");
                    return;
                }
            }

            if(TextUtils.isEmpty(password.getText().toString())){
                ToastUtils.showToast(mContext , "请输入新密码");
                return;
            }
            Call<ResultDO> call = null;
            if(getIntent().getIntExtra(BaseUtil.TYPE , -1) == 1){
               call = HttpClient.getHttpAdapter().updatePassword(BaseApp.getInstance().getMemberId() , validateCode ,oldpassword.getText().toString() ,password.getText().toString());
            }else {
                call = HttpClient.getHttpAdapter().forgetPassword(mobile , password.getText().toString() ,validateCode);
            }
            call.enqueue(new Callback<ResultDO>() {
                @Override
                public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                    if(response.body() == null || isFinish ){
                        return;
                    }
                    if(response.body().getCode() == 0 ){
                        SharePerferenceUtils.getIns(mContext).putBoolean(MemberConst.IS_LOGIN , false);
                        SharePerferenceUtils.getIns(mContext).putBoolean("is_loginSelf", false);
                        EventBus.getDefault().post(new ExitApp());
                        BaseUtil.openActivity(mContext, LoginActivity.class);
                        ToastUtils.showToast(mContext , "修改成功，请重新登录");
                    }else {
                        ToastUtils.showToast(mContext , response.body().getMessage());
                    }
                }

                @Override
                public void onFailure(Call<ResultDO> call, Throwable t) {

                }
            });

        }



//    boolean isVisiable = false;
//    @OnClick(R.id.password_visiable)
//    void passVisiable(){
//        isVisiable = !isVisiable;
//        if(isVisiable){
//            password_visiable.setImageResource(R.mipmap.update_password_invisible);
//            password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//        }else {
//            password_visiable.setImageResource(R.mipmap.update_password_visible);
//            password.setTransformationMethod(PasswordTransformationMethod.getInstance());
//        }
//
//        password.setSelection(password.getText().toString().length());
//    }

//    private boolean isuser = false;
//    private TextWatcher userWatcher = new TextWatcher() {
//        @Override
//        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//        }
//
//        @Override
//        public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//        }
//
//        @Override
//        public void afterTextChanged(Editable s) {
//            if(s.toString().length() >=6){
//                isuser = true;
//            }else {
//                isuser = false;
//            }
//
//            if(isuser){
//                btn.setClickable(true);
//                btn.setBackgroundResource(R.color.colorAccent);
//            }else {
//                btn.setClickable(false);
//                btn.setBackgroundResource(R.color.item_pressed);
//            }
//
//        }
//    };

}
